const httpNet = require("./httpUtils.js");
const app = getApp();
function loginx(that, cb) {
    console.log("去登陆....");
    if(!that){
        logina(cb);
        return;
    }
    // let uuid = wx.getStorageSync("uid");
    // console.log(uuid);
    // if(uuid){
    // 	httpNet.reqGet('xwx/getUserInfoByUid',{id:uuid},function(r){
    // 		console.log(r);
    // 	});
    // }
    // 获取登陆凭证
    wx.login({
        success(res) {
            if (res.code) {
                // 获取用户信息;
                wx.getUserInfo({
                    withCredentials: true,
                    success: data => {
                        console.log(data);
                        //app.globalData.userInfo = res.userInfo
                        // wx.showLoading({
                        //     title: '正在登陆',
                        // });
                        wx.request({
                            url: httpNet.wxLogin,
                            data: { code: res.code, iv: data.iv, encryptedData: data.encryptedData },
                            method: 'post',
                            header: {
                                'Content-Type': 'application/x-www-form-urlencoded',
                            },
                            success: function (res) {
                                console.log(res);

                                //取出来返回的数据的id放在缓存中,标识是当前用户的id
                                var dat = res.data;
                                if (dat.status == 1) {
                                    //将token放进缓存
                                    console.log("本次登陆返回的token:"+dat.data.token);
                                    wx.setStorageSync('token', dat.data.token);
                                    
                                    //将用户信息放进缓存
                                    // wx.showToast({
                                    //     title: '登录成功',
                                    //     icon: 'success',
                                    //     duration: 1000
                                    // });
                                    that.setData({
                                        utoken: dat.data.token,
                                    })
                                    getUserInfo(that);

                                    if (cb) {
                                        return typeof cb == "function" && cb(res.data)
                                    }
                                } else {
                                    wx.hideLoading();
                                    wx.showToast({
                                        title: '登录失败',
                                        icon: 'none',
                                        duration: 2000
                                    });
                                    that.setData({
                                        userInfo: false,
                                        hasUserInfo: false
                                    });
                                    wx.clearStorage();
                                }
                            },
                            fail: function () {
                                wx.showToast({
                                    title: '登录失败',
                                    icon: 'none',
                                    duration: 2000
                                });
                                wx.hideLoading();
                                that.setData({
                                    userInfo: false,
                                    hasUserInfo: false
                                });
                                wx.clearStorage();
                            }
                        })
                    },
                    fail: data => {
                        console.log(res);
                        wx.hideLoading();
                    }
                })
            } else {
                wx.hideLoading();
                console.log('登录失败！' + res.errMsg)
            }
        }
    })
}

function logina(cb) {
    console.log("去登陆2....");
    
    // 获取登陆凭证
    wx.login({
        success(res) {
            if (res.code) {
                // 获取用户信息;
                wx.getUserInfo({
                    withCredentials: true,
                    success: data => {
                        console.log(data);
                       
                        wx.request({
                            url: httpNet.wxLogin,
                            data: { code: res.code, iv: data.iv, encryptedData: data.encryptedData },
                            method: 'post',
                            header: {
                                'Content-Type': 'application/x-www-form-urlencoded',
                            },
                            success: function (res) {
                                console.log(res);

                                //取出来返回的数据的id放在缓存中,标识是当前用户的id
                                var dat = res.data;
                                if (dat.status == 1) {
                                    //将token放进缓存
                                    console.log("本次登陆返回的token2:"+dat.data.token);
                                    wx.setStorageSync('token', dat.data.token);
                                    
                                    //将用户信息放进缓存
                                    // wx.showToast({
                                    //     title: '登录成功',
                                    //     icon: 'success',
                                    //     duration: 1000
                                    // });
                                  
                                    getUserInfo();

                                    if (cb) {
                                        return typeof cb == "function" && cb(res.data)
                                    }
                                } else {
                                    wx.hideLoading();
                                    wx.showToast({
                                        title: '登录失败',
                                        icon: 'none',
                                        duration: 2000
                                    });
                                    that.setData({
                                        userInfo: false,
                                        hasUserInfo: false
                                    });
                                    wx.clearStorage();
                                }
                            },
                            fail: function () {
                                wx.showToast({
                                    title: '登录失败',
                                    icon: 'none',
                                    duration: 2000
                                });
                                wx.hideLoading();
                                that.setData({
                                    userInfo: false,
                                    hasUserInfo: false
                                });
                                wx.clearStorage();
                            }
                        })
                    },
                    fail: data => {
                        console.log(res);
                        wx.hideLoading();
                    }
                })
            } else {
                wx.hideLoading();
                console.log('登录失败！' + res.errMsg)
            }
        }
    })
}
//获取用户的信息
function getUserInfo(_this) {
    // wx.setStorageSync('token', "06fd261f96099e05a413b490c545d9bb");
    //获取用户基本信息
    httpNet.post(httpNet.myDetail, {}, function (res) {
        console.log(res);
        if (res.status == 1) {
            wx.setStorageSync('ascUser', res.data.user);
        }
        //获取用户的审核状态
        //此处可以加一个判断如果是已经审核通过,则不用去请求了
        //   httpNet.post(httpNet.enrollStatus, {}, function (da) {
        //     wx.setStorageSync('eStatus', da.data.user_enroll);
        //     _this.setData({
        //         eStatus:  da.data.user_enroll
        //     })
        //   })
    })

}

module.exports = {
    logina: logina,
    loginx: loginx
}